import { baseModule } from "@/base/module";
import { PKConfigManager } from "@/parkour/config";
import { Dimensions } from "@sapi-game/constants";
import { LazyLoader } from "@sapi-game/gameComponent/common/lazyLoader";
import {
    RegionTeamChooser,
    RegionTeamChooserData,
} from "@sapi-game/gameComponent/region/regionTeamChooser";
import { RegionTeamCleaner } from "@sapi-game/gameComponent/region/regionTeamCleaner";
import { GamePlayer } from "@sapi-game/gamePlayer/gamePlayer";
import { PlayerGroup } from "@sapi-game/gamePlayer/playerGroup";
import { Game, Region } from "@sapi-game/main";
import { DimensionIds } from "@sapi-game/utils/vanila-data";
import { ParkourGame } from "../parkourGame";
import { actionBarComponent } from "./components/actionbar";
import { PKConfigSetter } from "./components/configSetter";
import { GameStarter } from "./components/gameStarter";
import { InteractComponet } from "./components/interact";

export class PKPreParingState extends baseModule.State {
    readonly blueTeam = new PlayerGroup(GamePlayer);
    readonly redTeam = new PlayerGroup(GamePlayer);
    readonly region = new Region.SphereRegion(
        DimensionIds.Overworld,
        { x: 721, y: -60, z: 435 },
        20
    );

    //正在倒计时
    isCounting: boolean = false;

    override onEnter(): void {
        this.addComponent(LazyLoader, {
            dimensionId: DimensionIds.Overworld,
            pos: { x: 721, y: -60, z: 435 },
            onLoad: this.onLoad.bind(this),
            onUnload: this.onUnload.bind(this),
        });
    }

    onLoad(loader: LazyLoader) {
        //选队
        loader
            .addComponent(RegionTeamChooser, { config: buildConfig(this) })
            .addComponent(RegionTeamCleaner, {
                region: this.region,
                teams: [this.blueTeam, this.redTeam],
            })
            .addComponent(GameStarter)
            .addComponent(actionBarComponent)
            .addComponent(InteractComponet)
            .addComponent(PKConfigSetter);
    }

    onUnload() {
        this.blueTeam.clear();
        this.redTeam.clear();
        this.isCounting = false;
    }

    /**检查玩家是否足够开局 */
    checkPlayers() {
        const blue = this.blueTeam.validSize;
        const red = this.redTeam.validSize;
        if (Game.config.config.debugMode) {
            return blue + red > 0; //仅开发
        }
        return blue >= 1 && red >= 1;
    }

    startGame() {
        if (this.checkPlayers()) {
            Game.manager.startGame<ParkourGame>(ParkourGame, {
                redTeam: this.redTeam.clone(),
                blueTeam: this.blueTeam.clone(),
                pkConfig: PKConfigManager.get(),
                blockEnabled: this.getEffectBlockEnabled(),
            });
        } else {
            this.region.runCommandOnPlayers("title @s title 玩家数量不足");
        }
        const loader = this.getComponent(LazyLoader);
        loader.reload();
    }

    getEffectBlockEnabled() {
        return !!Dimensions.Overworld.getBlock({
            x: 727,
            y: -58,
            z: 433,
        })?.getRedstonePower();
    }
}

/**构建选队数据 */
function buildConfig(
    state: PKPreParingState
): RegionTeamChooserData<GamePlayer>[] {
    return [
        {
            team: state.blueTeam,
            region: new Region.CubeRegion(
                DimensionIds.Overworld,
                { x: 722, y: -58, z: 427 },
                { x: 721, y: -58, z: 428 }
            ),
            onJoin(p) {
                p.sendMessage("§b你已加入蓝队");
                p.player?.playSound("note.bell");
            },
            onEnter(p) {
                if (this.team.has(p)) {
                    p.actionbar("§b你已经在蓝队了");
                }
            },
        },
        {
            team: state.redTeam,
            region: new Region.CubeRegion(
                DimensionIds.Overworld,
                { x: 721, y: -58, z: 442 },
                { x: 722, y: -58, z: 443 }
            ),
            onJoin(p) {
                p.sendMessage("§c你已加入红队");
                p.player?.playSound("note.bell");
            },
            onEnter(p) {
                if (this.team.has(p)) {
                    p.actionbar("§c你已经在红队了");
                }
            },
        },
    ];
}
